define(['jquery', 'bootstrap', 'backend', 'table', 'form','editable', 'cookie'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'jdy/message/index',
                    add_url: 'jdy/message/add',
                    edit_url: 'jdy/message/edit',
                    del_url: 'jdy/message/del',
                    multi_url: 'jdy/message/multi',
                    table: 'jdy_message',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                showToggle: false,
                showExport: false,
                searchFormVisible: true,
                columns: [
                    [
                        //{checkbox: true},
                        {field: 'id', title: __('Id'), operate:false},
                        {field: 'anchang.name', title: __('Anchang.name'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {field: 'task.title', title: __('工作事项'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {field: 'maintenance.content', title: __('维修记录'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {field: 'content', title: __('留言内容'), operate:false},
                        {
                            field: 'content2',
                            title: __('回覆内容'),
                            editable: {
                                emptytext: '无',
                                rows:10,
                                tpl: "<textarea rows='10' cols='50'></textarea>",
                                success: function (response, newValue) {
                                    //$(".btn-refresh").trigger("click");
                                }
                            },
                            operate: false,
                            align: 'left',
                        },
                        {field: 'user', title: __('User_id'), operate:false},
                        {field: 'hcuser', title: __('Hcuser_id'), operate:false},
                        //{field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'anchang.id', title: __('Anchang.id')},
                        //{field: 'anchang.building_id', title: __('Anchang.building_id')},
                        //{field: 'anchang.room_id', title: __('Anchang.room_id')},
                        //{field: 'anchang.addr', title: __('Anchang.addr')},
                        //{field: 'anchang.remark', title: __('Anchang.remark')},
                        //{field: 'anchang.floor_no', title: __('Anchang.floor_no')},
                        //{field: 'anchang.contact1_id', title: __('Anchang.contact1_id')},
                        //{field: 'anchang.contact2_id', title: __('Anchang.contact2_id')},
                        //{field: 'anchang.companywindow_id', title: __('Anchang.companywindow_id')},
                        //{field: 'anchang.xianshi', title: __('Anchang.xianshi')},
                        //{field: 'anchang.xzq', title: __('Anchang.xzq')},
                        //{field: 'anchang.mapurl', title: __('Anchang.mapurl'), formatter: Table.api.formatter.url},
                        //{field: 'anchang.orderdate', title: __('Anchang.orderdate'), operate:'RANGE', addclass:'datetimerange'},
                        //{field: 'anchang.fee', title: __('Anchang.fee')},
                        //{field: 'anchang.fee_type', title: __('Anchang.fee_type')},
                        //{field: 'anchang.pay_date', title: __('Anchang.pay_date'), operate:'RANGE', addclass:'datetimerange'},
                        //{field: 'anchang.is_pay', title: __('Anchang.is_pay')},
                        //{field: 'anchang.create_time', title: __('Anchang.create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'anchang.update_time', title: __('Anchang.update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'anchang.contact1_name', title: __('Anchang.contact1_name')},
                        //{field: 'anchang.contact1_mobile', title: __('Anchang.contact1_mobile')},
                        //{field: 'anchang.contact2_name', title: __('Anchang.contact2_name')},
                        //{field: 'anchang.contact2_mobile', title: __('Anchang.contact2_mobile')},
                        //{field: 'anchang.type', title: __('Anchang.type')},
                        //{field: 'task.id', title: __('Task.id')},
                        //{field: 'task.anchang_id', title: __('Task.anchang_id')},
                        //{field: 'task.remind_type', title: __('Task.remind_type')},
                        //{field: 'task.remind_date', title: __('Task.remind_date'), operate:'RANGE', addclass:'datetimerange'},
                        //{field: 'task.replay', title: __('Task.replay')},
                        //{field: 'task.quotation_no', title: __('Task.quotation_no')},
                        //{field: 'task.work_no', title: __('Task.work_no')},
                        //
                        //{field: 'task.task_status', title: __('Task.task_status'), formatter: Table.api.formatter.status},
                        //{field: 'task.maintenanceuser_id', title: __('Task.maintenanceuser_id')},
                        //{field: 'task.handleuser_id', title: __('Task.handleuser_id')},
                        //{field: 'task.notifyuser_ids', title: __('Task.notifyuser_ids')},
                        //{field: 'task.share_to_comm', title: __('Task.share_to_comm')},
                        //{field: 'task.notify_msg', title: __('Task.notify_msg')},
                        //{field: 'task.acdevice_ids', title: __('Task.acdevice_ids')},
                        //{field: 'task.baogu', title: __('Task.baogu')},
                        //{field: 'task.images', title: __('Task.images'), formatter: Table.api.formatter.images},
                        //{field: 'task.price', title: __('Task.price')},
                        //{field: 'task.luruman', title: __('Task.luruman')},
                        //{field: 'task.create_time', title: __('Task.create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'task.update_time', title: __('Task.update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'maintenance.id', title: __('Maintenance.id')},
                        //{field: 'maintenance.anchang_id', title: __('Maintenance.anchang_id')},
                        //{field: 'maintenance.task_id', title: __('Maintenance.task_id')},
                        //{field: 'maintenance.images', title: __('Maintenance.images'), formatter: Table.api.formatter.images},
                        //{field: 'maintenance.switch', title: __('Maintenance.switch'), formatter: Table.api.formatter.toggle},
                        //{field: 'maintenance.price', title: __('Maintenance.price')},
                        //{field: 'maintenance.luruman', title: __('Maintenance.luruman')},
                        //{field: 'maintenance.managerman', title: __('Maintenance.managerman')},
                        //{field: 'maintenance.create_time', title: __('Maintenance.create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'maintenance.update_time', title: __('Maintenance.update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 选择隐藏字段事件，使用cookie存入
            table.on('column-switch.bs.table', function (e, json) {
                    var myColumns=[];
                    $.each(table.bootstrapTable('getHiddenColumns'), function(i, item) {
                         myColumns.push(item.field);
                    });
                    $.cookie('fa_jdy_message', JSON.stringify(myColumns), { expires: 365, path: '/'});
             });
             if($.cookie('fa_jdy_message')){
                //读取cookie隐藏字段
                $.each(JSON.parse($.cookie('fa_jdy_message')), function(i, item) {
                  table.bootstrapTable('hideColumn', item);
                });
             }

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});